<?php

class PaymentAPI {

    /**
     *
     * @var CI_Controller
     */
    private $CI;
    private $url_api_payment = 'http://api.gomobi.vn/api/';
    private $app_payment = 'coreuser';
    private $app_payment_key = 'sefdgh3LHqdnTqCg';
    private $last_link_request;

    public function set_provider($username, $provider, $service_name, $service_id) {
        $data = $this->_call_api_payment('payment', 'update_provider', array('username' => $username, 'provider' => $provider), $service_name, $service_id);
        if (empty($data) === FALSE) {
            $result_payment = json_decode($data, TRUE);
            if (is_array($result_payment) === TRUE) {
                if ($result_payment['code'] == 140) {
                    $output['status'] = 0;
                    $output['url'] = $this->last_link_request;
                    $output['content'] = $data;
                    return $output;
                }
            }
        }
        $output['status'] = 1;
        $output['url'] = $this->last_link_request;
        $output['content'] = $data;
        return $output;
    }

    public function get_register_sms($provider_id, $service_name, $service_id, $register_key = NULL) {
        $data = $this->_call_api_payment('payment', 'get_sms_registry', array('provider' => $provider_id, 'key' => $register_key), $service_name, $service_id);
        if ($data) {
            $result_payment = json_decode($data, TRUE);
            if (is_array($result_payment) === TRUE) {
                if ($result_payment['code'] == 200500) {
                    if (is_required($result_payment['data'],array('content', 'port'))) {
                        $result =array(
                            'type' => 'fee',
                            'sms_content' => $result_payment['data']['content'],
                            'sms_phone' => $result_payment['data']['port'],
                            'sms_message' => 'Gửi tin nhắn xác thực tài khoản',
                            'sms_description' => 'Gửi tin nhắn xác thực tài khoản'
                        );
                    }
                } elseif ($result_payment['code'] == 200502) {
                    $result =array(
                        'type' => 'free',
                        'server' => $result_payment['data']['server'],
                        'reg_money' => $result_payment['data']['reg_money']
                    );
                } elseif ($result_payment['code'] == 200503) {
                    $result =array(
                        'type' => 'active',
                        'server' => $result_payment['data']['server'],
                        'reg_money' => $result_payment['data']['reg_money']
                    );
                }
                if (empty($result) === FALSE) {
                    $output['status'] = 0;
                    $output['data'] = $result;
                    $output['url'] = $this->last_link_request;
                    $output['content'] = $data;
                    return $output;
                }
            }
        }
        $output['status'] = 1;
        $output['url'] = $this->last_link_request;
        $output['content'] = $data;
        return $output;
    }

    public function get_active_sms($username, $provider_id, $service_name, $service_id) {
        $data = $this->_call_api_payment('payment', 'get_sms_active', array('username' => $username, 'provider' => $provider_id, 'key' => $register_key), $service_name, $service_id);
        if ($data) {
            $result_payment = json_decode($data, TRUE);
            if (is_array($result_payment) === TRUE) {
                if ($result_payment['code'] == 200500) {
                    if (is_required($result_payment['data'][0],array('content', 'phone'))) {
                        $result =array(
                            'sms_content' => $result_payment['data'][0]['content'],
                            'sms_phone' => $result_payment['data'][0]['phone'],
                            'sms_message' => 'Kích hoạt tài khoản',
                            'sms_description' => 'Kích hoạt tài khoản',
                        );
                    }
                }

                if (empty($result) === FALSE) {
                    $output['status'] = 0;
                    $output['data'] = $result;
                    $output['url'] = $this->last_link_request;
                    $output['content'] = $data;
                    return $output;
                }
            }
        }
        $output['status'] = 1;
        $output['url'] = $this->last_link_request;
        $output['content'] = $data;
        return $output;
    }

    public function get_user_provider_service($username, $service_name, $service_id) {
        /* @var $cache_payment CI_Cache */
        $this->CI = & get_instance();
        $this->CI->load->model('../third_party/MeAPI/Models/PaymentModel');
        $this->CI->load->helper('provider');
        $cache_payment = $this->CI->cache->load('memcache', 'payment_info');
        $result = $cache_payment->store('MeAPI_ProviderInfo_User_Provider_' . $username, $this->CI->PaymentModel, 'get_user_provider',array($username), 300);
        if (empty($result) === FALSE && is_array($result) === TRUE) {
            $provider_id = $result[$service_name];
            $service_register = $result['app'];
            return array('provider' => $provider_id, 'service_register' => $service_register);
        }
        return FALSE;
    }

    public function get_provider_server($provider_id, $service_name, $service_id) {
        /* @var $cache_payment CI_Cache */
        $this->CI = & get_instance();
        $this->CI->load->model('../third_party/MeAPI/Models/PaymentModel');
        $this->CI->load->helper('provider');
        $cache_payment = $this->CI->cache->load('memcache', 'payment_info');
        $provider = $cache_payment->store('MeAPI_ProviderInfo_Provider_' . $provider_id, $this->CI->PaymentModel, 'get_provider',array(convert_provider($provider_id, $service_id)));
        if (empty($provider) === FALSE && is_array($provider) === TRUE) {
            if ($provider['server']) {
                $server_list = $cache_payment->store('MeAPI_ProviderInfo_Server_List_' . $provider['server'], $this->CI->PaymentModel, 'get_provider_list_by_server',array($provider['server']));
                if (empty($server_list) === FALSE) {
                    $result = array();
                    for ($i = 0, $c = count($server_list); $i < $c; $i++) {
                        if (is_numeric($server_list[$i]['provider_id'])) {
                            $result[] = revert_provider($server_list[$i]['provider_id'], $service_id);
                        }
                    }
                    return $result;
                }
            }
        }
        return FALSE;
    }

    private function _call_api_payment($control, $function, $params, $service_name, $service_id) {
        $params['service'] = $service_name;
        $this->last_link_request = $this->url_api_payment . '?control=' . $control . '&func=' . $function . '&' . http_build_query($params) . '&service_id_secondary=' . $service_id . '&app_secondary=' . $service_name . '&app=' . $this->app_payment . '&token=' . md5(implode('', $params) . $this->app_payment_key);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->last_link_request);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_TIMEOUT,2);
        $result = curl_exec($ch);
        return $result;
    }

}

?>
